﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PhongMachTu.OConnection;
using PhongMachTu.OModel;
namespace PhongMachTu.OForm
{
    public partial class FormUsers : Form
    {
        public FormUsers()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox1.Text;
            string pwd = textBox2.Text;
            int type = comboBox1.SelectedIndex;
            if (DataManipulate.addNewUser(username, pwd, type + 2))
            {
                MessageBox.Show("Tài khoản được tạo");
                refreshListUser();
            }
            else
            {
                MessageBox.Show("Không thành công, vui lòng thử lại sau");
            }

        }
        public void refreshListUser()
        {
            dsUser.Items.Clear();
            ArrayList users = DataManipulate.getAllUsers();
            foreach (User aUser in users)
            {
                ListViewItem lvi = new ListViewItem(aUser.Email);
                lvi.Tag = aUser;
                dsUser.Items.Add(lvi);
            }
        }

        private void FormUsers_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            FormManager.mainForm.Show();
            FormManager.formUser.Hide();
            textBox1.Text = "";
            comboBox1.SelectedIndex = -1;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dsUser.SelectedItems.Count < 0)
                return;
            ListViewItem lvItem = dsUser.SelectedItems[0];
            User selectUser = (User)lvItem.Tag;
            if (DataManipulate.deleteUser(selectUser.Id))
            {
                MessageBox.Show("Đã xóa tài khoản");
                refreshListUser();
            }
            else
            {
                MessageBox.Show("Vui lòng thử lại sau");
            }
        }
    }
}
